Add failing test for variance measurement with nested generic type aliases#62934
Open
eL1fe wants to merge 1 commit intomicrosoft:mainfrom
Open
Add failing test for variance measurement with nested generic type aliases#62934eL1fe wants to merge 1 commit intomicrosoft:mainfrom
eL1fe wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…iases (microsoft#60453) This test demonstrates incorrect variance computation for type aliases with deeply nested anonymous object types containing Arrays. The variance is incorrectly measured as Independent (meaning the type parameter is not used) instead of Covariant, causing type-unsafe assignments to be allowed.
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a failing test case for issue #60453.
Bug: Variance is incorrectly computed as
Independent(instead ofCovariant) for type aliases with deeply nested anonymous object types containing Arrays. This causes type-unsafe assignments to be silently allowed.Investigation Findings
Reproduction
Root Cause Analysis
Variance Computation: In
getVariancesWorker(checker.ts:24958), variance is computed by comparingType<markerSubType>vsType<markerSuperType>usingisTypeAssignableTo.The Problem: For deeply nested types, both comparisons incorrectly return
true:isTypeAssignableTo(Sub, Super)=true(correct → Covariant)isTypeAssignableTo(Super, Sub)=true(INCORRECT → adds Contravariant)This results in
Bivariantvariance, which then becomesIndependentafter the subsequent check.Cache Evidence: After comparison, the relation cache shows:
SuperToSub = 1 (Succeeded)— wrong!SuperToSub = 2 (Failed)— correctOrder Dependency: The bug manifests only when the complex type is used first. If a simpler generic type with object structure (e.g.,
type Y<T> = { x: Array<T> }) is used before the complex type, the bug disappears.Potential Cause:
checkTypeRelatedToreturnsresult !== Ternary.False(line 22404). This meansTernary.Unknown(value 1) is treated astrue. When structural comparison encounters edge cases during first use,Unknownmay be incorrectly interpreted as success.Workaround
Explicit variance annotation fixes the issue:
Test Plan
varianceNestedGenericTypeAlias.tsFixes #60453